<?php

class App_Model_DbTable_Image_Base extends Ext_Db_Table  
{  
    protected $_name = 'image_base';
    
    private static $_imageBase = array();
    
    public function getRowById($id)
    {
        if(!$id)
        {
            return "";
        }
        
        if(isset(self::$_imageBase['id'][$id]))
        {
            return self::$_imageBase['id'][$id];
        }
        
        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id = ? ", $id);
        $row = $this->fetchRow($safe);
        if (!$row) {
            return "";
        }
        $_imageBase = $row->toArray();

        self::$_imageBase['id'][$id] = $_imageBase;
        
        return $_imageBase;
    }
    
    public function getRowByMd5sum($md5sum)
    {
        if(!$md5sum)
        {
            return "";
        }
        
        if(isset(self::$_imageBase['md5sum'][$md5sum]))
        {
            return self::$_imageBase['md5sum'][$md5sum];
        }
        
        $filter = new Zend_Filter_StripTags();
        $md5sum = $filter->filter( (string)$md5sum );
        $row = $this->fetchRow("md5sum = '" . $md5sum. "'");
        if (!$row) {
            return "";
        }
        $imageFastdfs = $row->toArray();

        self::$_imageBase['md5sum'][$md5sum] = $imageFastdfs;
        $id = $imageFastdfs['id'];
        self::$_imageBase['id'][$id] = $imageFastdfs;
        
        return $imageFastdfs;
    }
    
    public function addNew($inputArray)
    {
        $row = array (
            'md5sum'     => $inputArray['md5sum'],
            'path'       => $inputArray['path'],
            'fileName'   => $inputArray['fileName'],
            'createTime' => $inputArray['createTime'],
        );
        try {
            $rows_affected = $this->insert($row);
            $last_insert_id = $this->getAdapter()->lastInsertId();
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }
        return $last_insert_id;
    }
}  

